package com.fingard.dsp;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

import com.fingard.db.JDBCHelper;


public class XmlHander {
	
	static String ORACLE_DIRER="oracle.jdbc.driver.OracleDriver";
	static String ORACLE_URL="jdbc:oracle:thin:@10.60.44.23:1521:atstest";
	static String ORACLE_USER="abictest";
	static String ORACLE_PASSWORD="123456";
    /*
	public static List<Map<String, Object>> getData(Connection conn1,String sql) throws Exception {
		PreparedStatement pstmt = null;
		ResultSet rs = null;
	    List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();
	    pstmt = conn1.prepareStatement(sql);	
		rs = pstmt.executeQuery();
	    while (rs.next()) {
	        Map<String, Object> map = new HashMap<String, Object>();
	        ResultSetMetaData rsmd = rs.getMetaData();
	        int cols = rsmd.getColumnCount();
	        for (int i = 1; i <= cols; i++) {
	          map.put(rsmd.getColumnLabel(i).toLowerCase(), rs.getObject(i)!=null&&rs.getObject(i).getClass().getName().equals("java.sql.Date") ?new Date(rs.getTimestamp(i).getTime()):rs.getObject(i));
	        }
	        rows.add(map);
	    }
	    if (rs != null) {
	        try {
	          rs.close();
	        }catch(Exception e){
	        	
	        }
	    }

	    if (pstmt != null) {
	      try {
	    	  pstmt.close();
	      }
	      catch (Exception ignore) {
	    	  
	      }
	    }
		return rows;  
	}*/

	public static void getmsg(String parentid,StringBuilder sb,String xmlname, Connection conn1) throws Exception {
	    String sql = "select (select count(*) from t_configuration r where  r.parentid=t.urid) count,t.* from t_configuration t where t.type = '"+xmlname+"' and t.parentid";
	    if(parentid.equals("")){
	    	sql+= "  is null";	
	    }else{
	    	sql+= " ='"+parentid+"'";	
	    }
	    List<Map<String, Object>> rows = JDBCHelper.execQuery(conn1,sql,new Object[]{});// getData(conn1,sql);
          for (Map<String, Object> map : rows) {
        	String path = map.get("path").toString();
			path = path.substring(path.lastIndexOf("|")+1);
			String urid = map.get("urid")!=null?map.get("urid").toString():"";
			String value = map.get("value")!=null?map.get("value").toString():"";//map.get("value").toString();
			String count = map.get("count").toString();
			sb.append("<"+path+"");
		    List<Map<String, Object>> rows1 =JDBCHelper.execQuery(conn1,"select * from t_CONFIGURATIONATTRIBUTE t where mainid = '"+urid+"'",new Object[]{}); // getData(conn1,"select * from t_CONFIGURATIONATTRIBUTE t where mainid = '"+urid+"'");
	        for (Map<String, Object> map11 : rows1) {	        
	        	sb.append(" "+map11.get("name")+"=\""+(map11.get("value")==null?"":map11.get("value"))+"\"");
	        }
	        if("0".equals(count)&&!"".equals(value)){
	        	sb.append(">"+value+"</"+path+">");
	        }else if ("0".equals(count)){
	        	sb.append("/>");
	        }
	        else{
	        sb.append(">");
	        getmsg(urid,sb,xmlname,conn1);
	        sb.append("</"+path+">");
          }
          }
	}
	
	public static String dbtoxml(String xmlname,Connection conn1,String root) throws Exception {
		StringBuilder sb=new StringBuilder();
		try{
		getmsg("",sb,xmlname,conn1);
		if("".equals(sb.toString())){
			return null;
		}
		}finally{
			if(conn1!=null){
		conn1.close();
			}
		}
		return "<?xml version=\"1.0\" encoding=\"gb2312\" ?><"+root+">"+sb.toString()+"</"+root+">";
	}

}
